package monster.com.cvh.activity.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.baidu.mobstat.StatService;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;

import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.permission.DefaultRationale;
import monster.com.cvh.permission.PermissionSetting;
import monster.com.cvh.util.ToastUtils;

/**
 * @author: 13580
 * @data: 2018/3/5
 * @function: 兼容6.0以上动态权限申请activity
 */

public abstract class PermissionActivity extends BaseActivity {
    private Rationale mRationale;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRationale = new DefaultRationale();
    }

    public void requestPermission(Action sucessAction, Action failedAction, String... permissions) {
        if (failedAction == null) {
            AndPermission.with(this)
                    .permission(permissions)
                    .rationale(mRationale)
                    .onGranted(sucessAction)
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(@NonNull List<String> permissions) {
                            ToastUtils.showShort(getResources().getString(R.string.failure));
                            if (AndPermission.hasAlwaysDeniedPermission(PermissionActivity.this, permissions)) {
                                new PermissionSetting(PermissionActivity.this).showSetting(permissions);
                            }
                            //TODO 在这里可以参照拒绝权限接下来该做的事件

                        }
                    }).start();
        } else {
            AndPermission.with(this)
                    .permission(permissions)
                    .rationale(mRationale)
                    .onGranted(sucessAction)
                    .onDenied(failedAction)
                    .start();
        }
    }

    protected void requestPermission(Action sucessAction, Action failedAction, String[]... permissions) {
        if (failedAction == null) {
            AndPermission.with(this)
                    .permission(permissions)
                    .rationale(mRationale)
                    .onGranted(sucessAction)
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(@NonNull List<String> permissions) {
                            ToastUtils.showShort(getResources().getString(R.string.failure));
                            if (AndPermission.hasAlwaysDeniedPermission(PermissionActivity.this, permissions)) {
                                new PermissionSetting(PermissionActivity.this).showSetting(permissions);
                            }
                            //TODO 在这里可以参照拒绝权限接下来该做的事件
                        }
                    })
                    .start();
        } else {
            AndPermission.with(this)
                    .permission(permissions)
                    .rationale(mRationale)
                    .onGranted(sucessAction)
                    .onDenied(failedAction)
                    .start();
        }
    }
    /**
     * 百度统计
     */
    @Override
    protected void onPause() {
        super.onPause();

        /**
         * 页面结束（每个Activity中都需要添加，如果有继承的父Activity中已经添加了该调用，那么子Activity中务必不能添加）
         * 不能与StatService.onPageStart一级onPageEnd函数交叉使用
         */
        StatService.onPause(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        /**
         * 页面起始（每个Activity中都需要添加，如果有继承的父Activity中已经添加了该调用，那么子Activity中务必不能添加）
         * 不能与StatService.onPageStart一级onPageEnd函数交叉使用
         */
        StatService.onResume(this);
    }
}
